// Copyright 2019 orivil.com. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found at https://mit-license.org.

package code

import (
	"gitee.com/orivil/morgine/bundles/admin/image"
	admin_sql "gitee.com/orivil/morgine/bundles/admin/sql"
	"gitee.com/orivil/morgine/bundles/sql"
	day_ticker "gitee.com/orivil/morgine/bundles/ticker/day"
	"gitee.com/orivil/morgine/log"
	"gitee.com/orivil/morgine/service"
	"gitee.com/orivil/morgine/xinit"
	container2 "gitee.com/tugoer/psy-testing/code/container"
	"gitee.com/tugoer/psy-testing/code/models"
	"time"
)

var SliderImageLabel = &image.Label{
	Key:         "mkhuYtwie729dolvpwuqh16738cjrfusi",
	Name:        "首页滚动栏",
	Width:       640,
	Height:      158,
	AllowedEdit: sql.True,
}

var AdvertisementImageLabel = &image.Label{
	Key:         "mvcnbjkfldoieu59t874jfdyqixbzfgdkgfp3",
	Name:        "广告图标",
	AllowedEdit: sql.True,
}

func XInit() {
	xinit.Initializer.Listen(xinit.AfterDatabase, func(container *service.Container) error {
		admin_sql.Gorm().AutoMigrate(&models.JsonData{})
		image.InitLabel(AdvertisementImageLabel, SliderImageLabel)
		return nil
	})
	xinit.Initializer.Listen(xinit.Initializing, func(container *service.Container) error {
		// 监听每天任务
		day_ticker.Runner.AddCallback(func(now *time.Time) {
			// 删除缓存
			err := container2.RemoveSubjects()
			if err != nil {
				log.Emergency.Println(err)
			}
		})
		return nil
	})
	xinit.Initializer.Listen(xinit.AddRoutes, func(container *service.Container) error {
		registerRoutes()
		return nil
	})

}
